package com.android.lovingpet.community.posting;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.teaminfo.TeamInfoActivity;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

    private Context context;
    private List<Post> posts;

    public CommentAdapter(List<Post> posts) {
        this.posts = posts;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.findViewById(R.id.iv_head).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition() - 1;
                Post post = posts.get(position);
                if (post.getUserType() == 1) {
                    Account account = Utility.getAccount(post.getUserId());
                    if (account != null) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("account", account);
                        context.startActivity(intent);
                    }
                } else {
                    Team team = Utility.getTeam(post.getUserId());
                    if (team != null) {
                        Intent intent = new Intent(context, TeamInfoActivity.class);
                        intent.putExtra("team", team);
                        context.startActivity(intent);
                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Post post = posts.get(position);
        holder.tvTime.setText(post.getCreateTime());
        holder.tvContext.setText(post.getContext());
        if (post.getUserType() == 1) {
            Account account = Utility.getAccount(post.getUserId());
            if (account != null) {
                Glide.with(context).load(account.getHead()).into(holder.ivHead);
                holder.tvName.setText(account.getName());
            }
        } else {
            Team team = Utility.getTeam(post.getUserId());
            if (team != null) {
                Glide.with(context).load(team.getImage()).into(holder.ivHead);
                holder.tvName.setText(team.getName());
            }
        }
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivHead;
        TextView tvName;
        TextView tvTime;
        TextView tvContext;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_variety);
            tvContext = itemView.findViewById(R.id.tv_context);
        }
    }
}
